package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Room;
import com.ruoyi.system.domain.vo.GuestVo;
import com.ruoyi.system.service.IRoomService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Guest;
import com.ruoyi.system.service.IGuestService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 客人信息Controller
 * 
 * @author 华馨悦
 * @date 2025-01-24
 */
@Controller
@RequestMapping("/system/guest")
public class GuestController extends BaseController
{
    private String prefix = "system/guest";

    @Autowired
    private IGuestService guestService;

    @Resource
    private IRoomService roomService;

    @RequiresPermissions("system:guest:view")
    @GetMapping()
    public String guest()
    {
        return prefix + "/guest";
    }

    /**
     * 查询客人信息列表
     */
    @RequiresPermissions("system:guest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Guest guest)
    {
        startPage();
        List<Guest> list = guestService.selectGuestList(guest);
        List<GuestVo> guestVos = new ArrayList<>();
        list.forEach(g -> {
            Room room = roomService.selectRoomById(g.getRoomId());
            GuestVo guestVo = new GuestVo();
            BeanUtils.copyProperties(g, guestVo);
            guestVo.setRoomName(Optional.ofNullable(room).map(Room::getNumber).orElse("房间信息已被销毁"));
            guestVos.add(guestVo);
        });
        return getDataTable(guestVos);
    }

    /**
     * 导出客人信息列表
     */
    @RequiresPermissions("system:guest:export")
    @Log(title = "客人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Guest guest)
    {
        List<Guest> list = guestService.selectGuestList(guest);
        ExcelUtil<Guest> util = new ExcelUtil<Guest>(Guest.class);
        return util.exportExcel(list, "客人信息数据");
    }

    /**
     * 新增客人信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        Room room = new Room();
        room.setStatus(0);
        mmap.put("roomList", roomService.selectRoomList(room));
        return prefix + "/add";
    }

    /**
     * 新增保存客人信息
     */
    @RequiresPermissions("system:guest:add")
    @Log(title = "客人信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Guest guest)
    {
        guest.setCreatedTime(DateUtils.getNowDate());
        guest.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(guestService.insertGuest(guest));
    }

    /**
     * 修改客人信息
     */
    @RequiresPermissions("system:guest:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Guest guest = guestService.selectGuestById(id);
        Room room = new Room();
        room.setStatus(0);
        mmap.put("roomList", roomService.selectRoomList(room));
        mmap.put("guest", guest);
        return prefix + "/edit";
    }

    /**
     * 修改保存客人信息
     */
    @RequiresPermissions("system:guest:edit")
    @Log(title = "客人信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Guest guest)
    {
        guest.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(guestService.updateGuest(guest));
    }

    /**
     * 删除客人信息
     */
    @RequiresPermissions("system:guest:remove")
    @Log(title = "客人信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(guestService.softDeleteGuestByIds(ids));
    }
}
